package com.atguigu.srb.core.controller.api;


import com.atguigu.common.result.R;
import com.atguigu.srb.base.util.JwtUtil;
import com.atguigu.srb.core.pojo.entity.BorrowInfo;
import com.atguigu.srb.core.service.BorrowInfoService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * <p>
 * 借款信息表 前端控制器
 * </p>
 *
 * @author pandengZhu
 * @since 2023-02-01
 */
@Api(tags = "借款信息")
@Slf4j
@RestController
@RequestMapping("api/core/borrowInfo")
public class BorrowInfoController {

    @Resource
    private BorrowInfoService borrowInfoService;

    @ApiOperation("获取借款额度")
    @GetMapping("/auth/getBorrowAmount")
    public R getBorrowAmount(HttpServletRequest request) {
        String token = request.getHeader("token");
        Long userId = JwtUtil.getUserId(token);
        BigDecimal borrowAmount = borrowInfoService.getBorrowerAmount(userId);
        return R.ok().data("borrowAmount", borrowAmount);
    }

    @ApiOperation("提交借款申请")
    @PostMapping("/auth/save")
    public R save(@RequestBody BorrowInfo borrowInfo, HttpServletRequest request) {
        String token = request.getHeader("token");
        Long userId = JwtUtil.getUserId(token);
        borrowInfoService.saveBorrowInfo(borrowInfo, userId);
        return R.ok().message("提交成功");
    }

    @ApiOperation("获取借款申请审批状态")
    @GetMapping("/auth/getBorrowInfoStatus")
    public R getBorrowInfoStatus(HttpServletRequest request) {
        String token = request.getHeader("token");
        Long userId = JwtUtil.getUserId(token);
        Integer status = borrowInfoService.getBorrowInfoStatusByUserId(userId);
        return R.ok().data("borrowInfoStatus", status);
    }

    @ApiOperation("获取借款分页列表")
    @GetMapping("/list/{page}/{limit}")
    public R list(@ApiParam(value = "页码", required = true)
                  @PathVariable Long page,
                  @ApiParam(value = "页数", required = true)
                  @PathVariable Long limit,
                  @ApiParam(value = "关键字", required = false)
                  @RequestParam String keyword) {

        Page<BorrowInfo> pageParam = new Page<>(page, limit);
        IPage<BorrowInfo> pageModel = borrowInfoService.listPage(pageParam, keyword);
        return R.ok().data("pageModel", pageModel);
    }
}

